/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lct.eq2.socket;

import com.lct.eq2.data.Attack;
import com.lct.eq2.data.Group;
import com.lct.eq2.data.Line;
import com.lct.eq2.gui.LiteGui;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Me
 */
public class CommSocketThread extends Thread {

    private Socket socket;
    private LiteGui parentGui;
    private InetAddress address;
    private boolean running;

    public CommSocketThread(LiteGui parentGui) throws IOException {
        socket = new Socket("bobeotm.ath.cx", 13267);
        this.running = true;
        this.parentGui = parentGui;
        this.address = socket.getInetAddress();
        this.parentGui.setConnected(this.socket.isConnected());
    }

    public void closeSocket() {
        try {
            this.socket.close();
            this.running = false;
        } catch (IOException ex) {
            Logger.getLogger(CommSocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendData(String data) throws IOException {
        //System.out.println("CommSocketThread::sendData() - sent: '" + data + "'");
        //this.socket.write(ByteBuffer.wrap(data.getBytes()));
    }
    
    public void sendData(Object data) throws IOException {
        //System.out.println("CommSocketThread::sendData() - sent: '" + data + "'");
        //this.socket.write(ByteBuffer.wrap(data.getBytes()));
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(data);
            oos.flush();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void run() {
        ObjectInputStream ois = null;
        while (socket.isConnected() && running) {
            long beforeReceive = System.currentTimeMillis();
            try {
                InputStream is = socket.getInputStream();
                if (is.available() > 0) {
                    ois = new ObjectInputStream(is);
                    try {
                        Object o = ois.readObject();
                        if (o instanceof Line || o instanceof Attack) {
                            //Line line = (Line)o;
                            //System.out.println(line.getCharacter());
                            if (o instanceof Attack) {
                                Attack a = (Attack)o;
                                
//                                a.setLagTime(a.getLagTime() + (System.currentTimeMillis() - beforeReceive));
                                this.parentGui.addDataToTail(a);
                            } else {
                                this.parentGui.addDataToTail(o);
                            }
                        } else if (o instanceof Group) {
                            Group g = (Group)o;
                            switch (g.getGroupType()) {
                                case Group.GROUP_CREATED:
                                    this.parentGui.createGroup(g.getGroupID());
                                    break;
                                case Group.GROUP_EXISTS:
                                    this.parentGui.groupAlreadyExists(g.getGroupID());
                                    break;
                                case Group.GROUP_JOINED:
                                    this.parentGui.joinGroup(g.getGroupID());
                                    break;
                                case Group.NO_GROUP_EXISTS:
                                    this.parentGui.groupDoesNotExist(g.getGroupID());
                                    break;
                                case Group.GROUP_PLAYER_LEFT:
                                    this.parentGui.reloadGroupMembers(g.getGroupMembers(), g.getGroupID());
                                    break;
                                case Group.GROUP_PLAYER_JOINED:
                                    this.parentGui.addToGroup((String)g.getGroupMembers().get(0));
                                    break;
                                default:
                                    break;
                            }
                        }
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CommSocketThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                Thread.currentThread().sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(CommSocketThread.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SocketException ex) {
                if (ex.getMessage().indexOf(" closed") > 0) {
                    try {
                        this.socket.close();
                    } catch (IOException e) {
                    //nothing
                    }
                } else {
                    Logger.getLogger(CommSocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.parentGui.setConnected(false);
            } catch (IOException ex) {
                if (ex.getMessage().indexOf(" closed") > 0) {
                    try {
                        this.socket.close();
                    } catch (IOException e) {
                        //nothing
                    }
                }
                this.parentGui.setConnected(false);
                Logger.getLogger(CommSocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.parentGui.setConnected(false);
    }
}
